package cn.kpi.service.attendance.impl;

import java.util.List;

import cn.kpi.dao.attendance.AttTbDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;

import cn.kpi.dao.attendance.AttBmHzDao;
import cn.kpi.entity.attendance.AttDetailEntity;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.service.attendance.IAttBmHzService;

@Service
public class AttBmHzServiceImpl implements IAttBmHzService  {
	@Autowired
	private AttBmHzDao attBmHzDao;
	@Autowired
	private AttTbDao attTbDao;
	
	public List<AttDetailEntity> GetAttDetailBySummayId(Long attsummaryid,Long orgsummaryid)throws Exception{
		try{
			return attBmHzDao.GetAttDetailBySummayId(attsummaryid,orgsummaryid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public int UpdateBmHz(List<AttDetailEntity> lst,Long attsummaryid)throws Exception{
		try{
			int result=0;
			
			float cqts=0;//出勤天数
			float jbts=0;//加班天数
			float xjts=0;//休假天数
			float tqjts=0;//探亲假
			float hjts=0;//婚假天数
			float bsjts=0;//病事假天数
			float bjts=0;//病假天数
			float sjts=0;//事假天数
			float gsycjts=0;//工伤、孕产假天数
			float gsts=0;//工伤天数
			float syjts=0;//孕产假天数
			float sangjts=0;//丧假天数
			float qtts=0;//其它天数
			int cdcc=0;//迟到次数
			int ztcc=0;//早退次数
			int kgcs=0;//旷工次数
			float wcqtsxj=0;//未出勤天数小计
			int mouth=lst.get(0).getMouth();
			Long userid=lst.get(0).getUserid();
			int year=lst.get(0).getYear();
			for(int i=0;i<lst.size();i++){
				AttDetailEntity item=lst.get(i);
				cqts=cqts+item.getSfcq();
				jbts=jbts+item.getSfjb();
				xjts=xjts+item.getNxj();//休假天数
				
				bsjts=bsjts+item.getBj()+item.getSj();
				bjts=bjts+item.getBj();//病假天数
				sjts=sjts+item.getSj();//事假天数
				
				gsycjts=gsycjts+item.getYgzs()+item.getSyj();
				gsts=gsts+item.getYgzs();//工伤天数
				syjts=syjts+item.getSyj();//孕产假天数
				cdcc=cdcc+item.getCd();
				ztcc=ztcc+item.getZt();
				kgcs=kgcs+item.getKg();
				
				hjts+=item.getHj();//婚假天数
				tqjts+=item.getTqj();//探亲假
				sangjts+=item.getSj();//丧假天数
				qtts+=item.getQt();//其它天数
				wcqtsxj+=item.getNxj()+item.getHj()+item.getTqj()+item.getSyj()+item.getSj()+item.getYgzs()+item.getBj()+item.getSj()+item.getQt();
				
			}
			cqts=cqts-wcqtsxj;
			int sfqq=1;
			if(cqts!=lst.size()){
				sfqq=0;
			}
			
			
			AttSummaryEntity sumentity=new AttSummaryEntity();
			sumentity.setBjts(bjts);
			sumentity.setBsjts(bsjts);
			//sumentity.setByuserid(byuserid);
			sumentity.setCdcc(cdcc);
			sumentity.setCqts(cqts);
			sumentity.setGsts(gsts);
			sumentity.setGsycjts(gsycjts);
			sumentity.setJbts(jbts);
			sumentity.setKgcs(kgcs);
			sumentity.setMouth(mouth);
			sumentity.setSjts(sjts);
			sumentity.setStatus(0);
			sumentity.setSyjts(syjts);
			sumentity.setUserid(userid);
			sumentity.setXjts(xjts);
			sumentity.setYear(year);
			sumentity.setZtcc(ztcc);
			
			sumentity.setSfqq(sfqq);
			sumentity.setTqjts(tqjts);
			sumentity.setHjts(hjts);
			sumentity.setSangjts(sangjts);
			sumentity.setQtts(qtts);
			sumentity.setWcqtsxj(wcqtsxj);
			sumentity.setAttsummaryid(attsummaryid);
			
			//System.out.println(JSON.toJSONString(sumentity));
			
			//System.out.println(JSON.toJSONString(lst));
			
			result+=attBmHzDao.UpdateAttBmHzSummary(sumentity);
			result+=attBmHzDao.UpdateDetailBatch(lst);
			if(result<=1){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
			
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	public int UpdateBmHzNoDetail(AttSummaryEntity entity)throws Exception{
		try{
			return attBmHzDao.UpdateAttBmHzSummary(entity);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	
	@Transactional
	public int UpdateAttStatus(Long orgsummaryid,int status)throws Exception{
		try{
			int result=0;
			result+=attBmHzDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			result+=attBmHzDao.UpdateAttSummaryStatus(orgsummaryid, status);
			result+=attBmHzDao.UpdateAttDetailStatus(orgsummaryid, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Transactional
	public int UpdateAttStatusAppUser(Long orgsummaryid,Long approverUser,int status)throws Exception{
		try{
			int result=0;
			result+=attBmHzDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			result+=attBmHzDao.UpdateAttSummaryStatus(orgsummaryid, status);
			result+=attBmHzDao.UpdateAttDetailStatus(orgsummaryid, status);
			result+= attTbDao.UpdateApproverUser(approverUser,orgsummaryid);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	
	public int GetQqDaysByYearAndMonth(int year,int month)throws Exception{
		try{
			return attBmHzDao.GetQqDaysByYearAndMonth(year,month);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
}
